package com.egao.common.manage.company;

import com.egao.common.core.annotation.ApiPageParam;
import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.company.constants.CompanyConstants;
import com.egao.common.module.company.entity.vo.*;
import com.egao.common.module.company.service.MutualApplyService;
import com.egao.common.module.company.service.MutualService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 校内双选会管理
 * @author dl
 */
@Api(tags = "校内双选会管理")
@RestController
@RequestMapping("${api}")
public class CompanyMutualController extends BaseController {

    @Autowired
    private MutualApplyService mutualApplyService;

    @Autowired
    private MutualService mutualService;

    @PreAuthorize("hasAuthority('company')")
    @ApiOperation(value = "双选会信息", notes = "未审核状态升序 创建时间倒序")
    @OperLog(value = "双选会信息", desc = "分页查询双选会信息")
    @ApiPageParam
    @GetMapping("/company/mutual/page")
    public PageResult<MutualApplyListVO> page(HttpServletRequest request) {
        PageParam<MutualApplyListVO> pageParam = new PageParam<>(request);
        pageParam.put("userId", this.getLoginUserId());
        return new PageResult<>(mutualApplyService.listPage(pageParam), pageParam.getTotal());
    }

    @PreAuthorize("hasAuthority('company')")
    @OperLog(value = "添加双选会信息", desc = "添加双选会信息", result = true)
    @ApiOperation(value = "添加双选会信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mutualApplyFormVO", value = "mutualApplyFormVO", required = true, dataType = "MutualApplyFormVO")
    })
    @PostMapping("/company/mutual/save")
    public JsonResult save(@Valid @RequestBody MutualApplyFormVO mutualApplyFormVO) {
        mutualApplyFormVO.setCreateBy(getLoginUserId());
        // 设置双选会信息为待审核
        mutualApplyFormVO.setState(CompanyConstants.RECRUIT_STATE_DSH);
        return mutualApplyService.saveMutualApply(mutualApplyFormVO);
    }

    @PreAuthorize("hasAuthority('company')")
    @OperLog(value = "修改双选会信息", desc = "修改双选会信息", result = true)
    @ApiOperation(value = "修改双选会信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mutualApplyFormVO", value = "mutualApplyFormVO", required = true, dataType = "MutualApplyFormVO")
    })
    @PutMapping("/company/mutual/update")
    public JsonResult update(@Valid @RequestBody MutualApplyFormVO mutualApplyFormVO) {
        return mutualApplyService.updateMutualApply(mutualApplyFormVO);
    }

    @PreAuthorize("hasAuthority('company')")
    @OperLog(value = "取消双选会信息", desc = "取消双选会信息", result = true)
    @ApiOperation(value = "取消双选会信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mutualApplyFormVO", value = "mutualApplyFormVO", required = true, dataType = "mutualApplyFormVO")
    })
    @PutMapping("/company/mutual/cancel")
    public JsonResult cancel(@Valid @RequestBody MutualApplyFormVO mutualApplyFormVO) {
        return mutualApplyService.cancelMutualApply(mutualApplyFormVO);
    }

    @PreAuthorize("hasAuthority('company')")
    @OperLog(value = "删除双选会信息", desc = "删除双选会信息", result = true)
    @ApiOperation(value = "删除双选会信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mutualApplyFormVO", value = "mutualApplyFormVO", required = true, dataType = "MutualApplyFormVO")
    })
    @PutMapping("/company/mutual/delete")
    public JsonResult deleteMutualApply(@RequestBody MutualApplyFormVO mutualApplyFormVO) {
        return mutualApplyService.deleteMutualApply(mutualApplyFormVO);
    }


    @PreAuthorize("hasAuthority('company')")
    @ApiOperation(value = "获取双选会申请信息详情")
    @OperLog(value = "获取双选会申请信息详情", desc = "获取双选会申请信息详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "双选会申请信息id", required = true, dataType = "int")
    })
    @GetMapping("/company/mutual/detail")
    public JsonResult detail(Integer id) {
        return mutualApplyService.detail(id);
    }

    @PreAuthorize("hasAuthority('company')")
    @ApiOperation(value = "获取双选会信息详情")
    @OperLog(value = "获取双选会信息详情", desc = "获取双选会信息详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "双选会信息id", required = true, dataType = "int")
    })
    @GetMapping("/company/mutual/one")
    public JsonResult one(Integer id) {
        return mutualService.detail(id);
    }
    
}
